package com.changgou.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.dao.ESManagerMapper;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.pojo.SkuInfo;
import com.changgou.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: changgou_parent
 * @Package: com.changgou.service.impl
 * @ClassName: ESManagerServiceImpl
 * @Author: 孙明田
 * @Description:
 * @Date: 2020/6/19 15:50
 * @Version: 1.0
 */

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private ESManagerMapper esManagerMapper;


    /**
     * 创建索引结构
     */
    @Override
    public void createIndexAndMapping() {
        //创建索引库
        elasticsearchTemplate.createIndex(SkuInfo.class);

        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }


    //将所有数据导入
    @Override
    public void importAll() {
        //1.远程调用feign接口查询sku列表集合
        List<Sku> skuList = skuFeign.findBySpuId("all");
        //2.判断集合是否为空
        if (skuList == null && skuList.size() <= 0) {
            throw new RuntimeException("无法查到商品，禁止导入");
        }
        //3.将skuList集合转化为json
        String skuJson = JSON.toJSONString(skuList);
        //4.将skuJson转化为skuInfo对象
        List<SkuInfo> skuInfoList = JSON.parseArray(skuJson, SkuInfo.class);
        //5.遍历skuInfo集合
        for (SkuInfo skuInfo : skuInfoList) {
            //将skuInfo对象中的spec规格信息转为json
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //执行导入数据操作
        esManagerMapper.saveAll(skuInfoList);

    }

    //根据spuId查询skuList，导入到索引库
    public void importDataBySpuId(String spuId) {
        //1.远程调用feign接口查询sku列表集合
        List<Sku> skuList = skuFeign.findBySpuId(spuId);
        //2.判断集合是否为空
        if (skuList == null && skuList.size() <= 0) {
            throw new RuntimeException("无法查到商品，禁止导入");
        }
        //3.将skuList转为json
        String skuJson = JSON.toJSONString(skuList);
        //4.将json转为skuInfo对象
        List<SkuInfo> skuInfoList = JSON.parseArray(skuJson, SkuInfo.class);
        //5.遍历集合
        for (SkuInfo skuInfo : skuInfoList) {
            //将规格参数转为json
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //6.将数据导入到索引库
        esManagerMapper.saveAll(skuInfoList);

    }

    @Override
    public void deleteDataBySpuId(String spuId) {
        //1.远程调用feign接口查询sku列表集合
        List<Sku> skuList = skuFeign.findBySpuId(spuId);
        //2.判断集合是否为空
        if (skuList == null && skuList.size() <= 0) {
            throw new RuntimeException("无法查到商品，删除失败");
        }

        //遍历集合的得到每个sku对象
        for (Sku sku : skuList) {
            //6.根据skuId删除对应信息
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }

    }


}
